package BlockingQueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 模拟一个简易 线程池(自己写的，可能有错)
 */


class MyThreadPool{
    //管理任务的队列
    private BlockingQueue<Runnable> taskBlockingQueue=new LinkedBlockingQueue();

    //传入这个线程应该要多少个线程池
    public MyThreadPool(int n) {
        for(int i=0;i<n;i++){
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {

                    //  这个线程是一直在工作的，他要循环的获取队列里的任务
                    while(true){
                        try {
                            Runnable task=taskBlockingQueue.take();
                            task.run();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            });
            thread.start();
        }
    }

    public void  submit(Runnable task) {
        try {
            taskBlockingQueue.put(task);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class Demo3 {
    public static void main(String[] args) {

        MyThreadPool myThreadPool=new MyThreadPool(10);
        while(true){
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello");
                }
            });

        }

    }


}














